package com.dqhl.community.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dqhl.community.R;
import com.dqhl.community.adapter.FamilyProjectAdapter;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.model.FamilyProjectBean;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.Constant;
import com.dqhl.community.util.JsonUtils;
import com.dqhl.community.util.ScreenUtils;
import com.dqhl.community.view.LuocangDialog;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * Created by LIENLIN on 2016/8/30.
 * 家政服务
 */
public class FamilyServiceActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private ImageView mIvback;
    private TextView mTvTitle;
    private TextView mTvAllMoney;//维修总金额
    private GridView mGvProject;
    private TextView mTvAccount;
    private TextView mTvBaoJie;
    private TextView mTvPrice;//
    private TextView mTvLocation;
    private TextView mTvNoData;//网络异常时显示
    private EditText mEtPhone;
    private EditText mEtName;
    private EditText mEtAddRess;
    private ImageView mImOperation;
    private ImageView mImJian;//减号
    private ImageView mImAdd;//加号
    private TextView mTvNumber;//商品个数
    private ScrollView mSv;
    private RelativeLayout mRelativeLayout;//定位点击事件
    private List<FamilyProjectBean> familyProjectBeen;
    private String project_id;//家政项目id
    private String money = "0";//单价
    private int num = 1;
    private static int REQUEST_CODE = 100;
    private String location = "";
    private String communityId = "";
    private LinearLayout ll_add_gouwuche;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_family_service);


        initView();
//        location = spUserInfo.getString("location", "请选择小区");
//        communityId = spUserInfo.getString("communityId", "");
//        mTvLocation.setText(location);
//        getDate(communityId);
        //进入界面需先定位小区
        final LuocangDialog dialog = new LuocangDialog(FamilyServiceActivity.this, "", "请先定位所在小区", "确认");
        dialog.setButtonClickListener(new LuocangDialog.ButtonClickListener() {
            @Override
            public void doConfirm() {
                dialog.dismiss();
                overlay(LocationClearActivity.class, REQUEST_CODE);
            }
        });
        dialog.show();
    }

    private void initView() {
        mIvback = (ImageView) findViewById(R.id.iv_top_back);
        mTvTitle = (TextView) findViewById(R.id.tv_top_title);
        mTvAllMoney = (TextView) findViewById(R.id.tv_shopping_allmoney);
        mImOperation = (ImageView) findViewById(R.id.tv_top_operation);
        mImJian = (ImageView) findViewById(R.id.iv_family_jian);
        mImAdd = (ImageView) findViewById(R.id.iv_family_add);
        mGvProject = (GridView) findViewById(R.id.gv_family_project);
        mTvNumber = (TextView) findViewById(R.id.tv_family_number);
        mTvAccount = (TextView) findViewById(R.id.tv_account);
        mTvBaoJie = (TextView) findViewById(R.id.tv_baojie);
        mTvPrice = (TextView) findViewById(R.id.tv_family_price);
        mTvLocation = (TextView) findViewById(R.id.tv_family_location);
        mTvNoData = (TextView) findViewById(R.id.tv_familyservice_nodata);
        mEtPhone = (EditText) findViewById(R.id.et_family_phone);
        mEtName = (EditText) findViewById(R.id.et_family_name);
        mEtAddRess = (EditText) findViewById(R.id.et_family_address);
        mSv = (ScrollView) findViewById(R.id.sv_family_scrollview);
        mRelativeLayout = (RelativeLayout) findViewById(R.id.relative_dingwei);
        ll_add_gouwuche = (LinearLayout) findViewById(R.id.ll_add_gouwuche);
        ll_add_gouwuche.setVisibility(View.GONE);
        //动态设置高度
        int height = ScreenUtils.getScreenHeight(this);
        int px = ScreenUtils.dp2px(this, 130);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mSv.getLayoutParams();
        params.height = height - px;
        mSv.setLayoutParams(params);

        mIvback.setVisibility(View.VISIBLE);
        mImOperation.setVisibility(View.INVISIBLE);
        mIvback.setOnClickListener(this);
        mImJian.setOnClickListener(this);
        mImAdd.setOnClickListener(this);
        mTvAccount.setOnClickListener(this);
        mTvLocation.setOnClickListener(this);
        mRelativeLayout.setOnClickListener(this);
        mTvTitle.setText("家政服务");

        mGvProject.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
            case R.id.tv_account:

                //String name,String phone,String address,String project_id,String userid
                String name = mEtName.getText().toString();
                String phone = mEtPhone.getText().toString();
                String address = mEtAddRess.getText().toString();
                String userid = spUserInfo.getString(Constant.USERID, "");
                Log.d("FamilyServiceActivity", "project_id=" + project_id);
                Log.d("FamilyServiceActivity", "userid=" + userid);
                Log.d("FamilyServiceActivity", "location=" + location);
                if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(phone) && !TextUtils.isEmpty(address) && !TextUtils.isEmpty(userid) && !TextUtils.isEmpty(project_id) && !TextUtils.isEmpty(location)) {
                    commitDate(name, phone, address, project_id, userid, location);
                } else {
                    toast("请完善信息");
                }

                break;
            case R.id.iv_family_jian:
                if (num > 1) {
                    mTvNumber.setText(--num + "");
                    mTvAllMoney.setText("¥" + Integer.valueOf(money) * num);
                } else {
                    num = 0;
                    mTvNumber.setVisibility(View.GONE);
                    mImJian.setVisibility(View.GONE);
                    mTvAllMoney.setText("¥" + Integer.valueOf(money) * num);
                }
                break;
            case R.id.iv_family_add:

                if (!TextUtils.isEmpty(project_id)) {
                    if (num == 0) {
                        mTvNumber.setVisibility(View.VISIBLE);
                        mImJian.setVisibility(View.VISIBLE);
                    }
                    mTvNumber.setText(++num + "");
                    mTvAllMoney.setText("¥" + Integer.valueOf(money) * num);
                }

                break;
            case R.id.tv_family_location:
                overlay(LocationClearActivity.class, REQUEST_CODE);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
                Bundle bundle = data.getExtras();
                location = bundle.getString("community_name");
                communityId = bundle.getString("community_id");
//                SharedPreferences.Editor edit = spUserInfo.edit();
//                edit.putString("location",location);
//                edit.putString("communityId",communityId);
//                edit.apply();
                Log.d("onActivityResult", "bundle.getString(community_name)=" + location);
                mTvLocation.setText(location);
                getDate(communityId);
            }
        }
    }

    private void getDate(String communityId) {
        Log.d("FamilyServiceActivity", "communityId=" + communityId);
        mImAdd.setVisibility(View.GONE);
        mImJian.setVisibility(View.GONE);
        mTvNumber.setVisibility(View.GONE);
        mTvBaoJie.setVisibility(View.GONE);
        mTvPrice.setVisibility(View.GONE);
        mTvAllMoney.setText("¥" + 0);
        num = 1;
        mGvProject.setAdapter(null);
        mTvNoData.setVisibility(View.GONE);
        RequestParams params = new RequestParams(Config.Api.family_project);
        params.addBodyParameter("community_id", communityId);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d("FamilyServiceActivity", result);
                int errCode = JsonUtils.getErrCode(result);
                if (errCode == 0) {
                    String data = JsonUtils.getInfo(result, "data");
                    Log.d("FamilyServiceActivity", data);
                    familyProjectBeen = JSON.parseArray(data, FamilyProjectBean.class);
                    if (familyProjectBeen.size() > 0) {
                        mGvProject.setAdapter(new FamilyProjectAdapter(FamilyServiceActivity.this, familyProjectBeen));
                    } else {
                        mTvNoData.setVisibility(View.VISIBLE);
                        mTvNoData.setText(R.string.error_no_info);
                    }
                } else {
                    mTvNoData.setVisibility(View.VISIBLE);
                    mTvNoData.setText(R.string.error_server_connection);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                mTvNoData.setVisibility(View.VISIBLE);
                mTvNoData.setText(R.string.error_net_connection);
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }

    private void commitDate(String name, String phone, String address, String project_id, String userid, String location) {
        RequestParams params = new RequestParams(Config.Api.family_service);
        params.addBodyParameter("name", name);
        params.addBodyParameter("phone", phone);
        params.addBodyParameter("address", address);
        params.addBodyParameter("project_id", project_id);
        params.addBodyParameter("userid", userid);
        params.addBodyParameter("location", location);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d("FamilyServiceActivity", "result=" + result);
                int errCode = JsonUtils.getErrCode(result);
                if (errCode == 0) {
                    String errMsg = JsonUtils.getInfo(result, "errMsg");
                    Log.d("FamilyServiceActivity", "errMsg=" + errMsg);
                    final LuocangDialog dialog = new LuocangDialog(FamilyServiceActivity.this, errMsg, "我们会尽快联系您", "确认");
                    dialog.setButtonClickListener(new LuocangDialog.ButtonClickListener() {
                        @Override
                        public void doConfirm() {
                            overlay(FamilyServiceRecordActivity.class);
                            dialog.dismiss();
                        }
                    });
                    dialog.show();
                } else {
                    toast("参数有误");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                toast(R.string.error_net_connection);
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        FamilyProjectBean bean = (FamilyProjectBean) adapterView.getItemAtPosition(i);
        mImAdd.setVisibility(View.VISIBLE);
        mImJian.setVisibility(View.VISIBLE);
        mTvNumber.setVisibility(View.VISIBLE);
        mTvNumber.setText(num + "");
        mTvBaoJie.setVisibility(View.VISIBLE);
        mTvPrice.setVisibility(View.VISIBLE);
        mTvBaoJie.setText(bean.getType());
        mTvPrice.setText("¥" + bean.getMoney());
        project_id = bean.getId();
        money = bean.getMoney();
        mTvAllMoney.setText("¥" + Integer.valueOf(money) * num);
    }
}
